package com.app.base.dialog;

import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

/**
 * Created by HJ on 2016-05-13.
 * dialog工厂类
 */
public class DialogFactory {

    private static final String DIALOG_CONFIRM_TAG = "confirm";
    private FragmentManager mFragmentManager;
    private DialogFragment dialogFragment;
    private LoadingFragment loadingFragment;

    public DialogFactory(FragmentManager fragmentManager) {
        this.mFragmentManager = fragmentManager;
    }

    /**
     * 显示确认对话框，dialogId是用来区分不同对话框的
     *
     * @param flag
     * @param okListener
     * @param cancelListener
     */
    public void showConfirmDialog(int flag, ConfirmDialogFragment.CommonDialogListener okListener, ConfirmDialogFragment.CommonDialogListener cancelListener) {
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        Fragment fragment = mFragmentManager.findFragmentByTag(DIALOG_CONFIRM_TAG);
        if (null != fragment) {
            ft.remove(fragment);
        }
        dialogFragment = ConfirmDialogFragment.newInstance(flag);
        ((ConfirmDialogFragment) dialogFragment).onReceiveDialogListener(okListener, cancelListener);
        dialogFragment.show(mFragmentManager, DIALOG_CONFIRM_TAG);
        mFragmentManager.executePendingTransactions();
    }

    /**
     * 取消确认对话框
     */
    public void dismissDialog() {
        if (dialogFragment != null) {
            dialogFragment.dismiss();
        }
    }

    /**
     * 显示加载对话框
     * @param tag
     */
    public void showLoadingFragment(String tag) {
        loadingFragment = LoadingFragment.newInstance();
        if (loadingFragment != null) {
            loadingFragment.show(mFragmentManager, tag);
        }
    }

    /**
     * 取消加载对话框
     */
    public void dismissLoadingFragment() {
        if (loadingFragment != null) {
            loadingFragment.dismiss();
        }
    }

}
